# coding=utf-8
import os
from string import Template

from utils.util import writeFile

common_h = Template("""
#pragma once

#include <cstdint>
#include <vector>
#include <string>

#define SERVICE_IP ecarx::transfer::ipcp::GetVPIP()
#define SERVICE_PORT 50500
#define MY_LOCAL_IP ecarx::transfer::ipcp::GetAPIP()
#define MY_LOCAL_PORT 50335
#define MY_VLAN_ID 0
#define MY_VLAN_PRIORITY 0
#define APVP_SERVER_IP "198.18.34.1"
#define APVP_IHU_IP "198.18.34.15"
#define APVP_MOCKTEST_IP "127.0.0.1"

namespace ecarx::transfer::ipcp{

    struct IpcpValue {
        uint16_t service_id;
        uint16_t oper_id;
        uint8_t oper_type;
        uint8_t payload_type;
        void* payload;
        std::size_t payload_len;
        char peer_ip[16];
        uint16_t peer_port;
    };

    bool IsDebugMode();
    const char* GetVPIP();
    const char* GetAPIP();
}
""")

common_cpp = Template("""
#include "common.h"
#include <android-base/properties.h>

namespace ecarx::transfer::ipcp {
    bool IsDebugMode() {
        return android::base::GetProperty("persist.vendor.ecarx.vhal.propconfig.runmode", "aosp") != "aosp";
    }

    const char* GetVPIP() {
        return IsDebugMode() ? APVP_MOCKTEST_IP : APVP_SERVER_IP;
    }

    const char* GetAPIP() {
        return IsDebugMode() ? APVP_MOCKTEST_IP : APVP_IHU_IP;
    }
}
""")

def gen_ipcp_common_h(outDir:str):
    writeFile(os.path.join(outDir,"src"), "common.h",lambda file:
        file.write(common_h.substitute()))
    writeFile(os.path.join(outDir,"src"), "common.cpp",lambda file:
        file.write(common_cpp.substitute()))


